//
//  L_RecommendBarViewController.m
//  音悦咖
//
//  Created by dllo on 15/7/14.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "L_RecommendBarViewController.h"
#import "MBProgressHUD.h"
@interface L_RecommendBarViewController () <MBProgressHUDDelegate>

@property (nonatomic, retain) MBProgressHUD *mbProgress;

@end

@implementation L_RecommendBarViewController


- (void)dealloc
{
    [_mbProgress release];
    [_recommendCollectionView release];
    [_songlistsArr release];
    [_titleArr release];
    [_posterArr release];
    [_hotmusicArr release];
    [_newestMusicArr release];
    [_selfdomRecommendArr release];
    [_smallRecommendArr release];
    [_AsianNewMusicArr release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor clearColor];
    
    [self judgeConnection];
    [self getData];
    [self createView];
}

#pragma mark - 判断是否有网络连接
- (void)judgeConnection
{
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        NSLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status));
        if ([AFStringFromNetworkReachabilityStatus(status) isEqualToString:@"Not Reachable"]) {
            
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"当前网络没有连接" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alert show];
            [alert release];
        } else if ([AFStringFromNetworkReachabilityStatus(status) isEqualToString:@"Reachable via WWAN"]){
            
            [self createProgress];
            //            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"当前正使用数据流量" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
            //            [alert show];
            //            [alert release];
        }else if ([AFStringFromNetworkReachabilityStatus(status) isEqualToString:@"Reachable via WiFi"]){
            
            //            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"当前网络为wifi网络" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
            //            [alert show];
            //            [alert release];
            [self createProgress];
        }
        
    }];
    
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    
}

#pragma mark - 获取数据
- (void)getData
{
    
    // 推荐主页链接
    NSString *str = @"http://online.dongting.com/recomm/recomm_modules?f=f168&v=v7.9.3.2015052719&rom=Xiaomi%252F2014011%252FHM2014011%253A4.2.2%252FHM2014011%252FJHFCNBH24.0%253Auser%252Frelease-keys&s=s200&deviceId=865763026833654&userId=0&version=1&utdid=U%2FXnNdXR1gwDAAnICPQYXIEM";
    
    [AFNetworkHandler GETWithAFNByURL:str completion:^(id result) {

        self.titleArr = [[result objectForKey:@"data"] objectForKey:@"songlists"];
        
        // 海报数据
        NSArray *tempArr1 = [[self.titleArr objectAtIndex:0] objectForKey:@"data"];
        self.posterArr = [L_PosterModel arrayWithModelByArray:tempArr1];
        
        
        // 热门歌单
        NSArray *tempArr2 = [[self.titleArr objectAtIndex:1] objectForKey:@"data"];
        self.hotmusicArr = [L_PosterModel arrayWithModelByArray:tempArr2];
        
        // 最新音乐
        NSArray *tempArr3 = [[self.titleArr objectAtIndex:2] objectForKey:@"data"];
        self.newestMusicArr = [L_PosterModel arrayWithModelByArray:tempArr3];
        
        // 个性推荐
        NSArray *tempArr4 = [[self.titleArr objectAtIndex:5] objectForKey:@"data"];
        self.selfdomRecommendArr = [L_PosterModel arrayWithModelByArray:tempArr4];
        
        // 冰凉小曲 清凉一下
        NSArray *tempArr5 = [[self.titleArr objectAtIndex:3] objectForKey:@"data"];
        self.smallRecommendArr = [L_PosterModel arrayWithModelByArray:tempArr5];
        
        // 亚洲新歌榜
        NSArray *tempArr6 = [[self.titleArr objectAtIndex:4] objectForKey:@"data"];
        self.AsianNewMusicArr = [L_PosterModel arrayWithModelByArray:tempArr6];
        
        // 获取完数据隐藏菊花
        self.mbProgress.hidden = YES;
        
        [self.recommendCollectionView reloadData];
    }];
    


}


- (void)createProgress
{
    self.mbProgress = [[MBProgressHUD alloc] initWithView:self.view];
    self.mbProgress.labelText = @"稍等";
    self.mbProgress.detailsLabelText = @"正在加载…";
    self.mbProgress.delegate = self;
    [self.mbProgress show:YES];
    [self.view addSubview:self.mbProgress];
}

- (void)doSomething
{
    //你要进行的一些事
    sleep(2);
}

#pragma mark - 创建视图
- (void)createView
{
    
    // 创建布局
    UICollectionViewFlowLayout *recommendLayout = [[[UICollectionViewFlowLayout alloc] init] autorelease];
    recommendLayout.sectionInset = UIEdgeInsetsMake(5, 0, 5, 0);
    
    recommendLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    // 创建collectionView
    
    self.recommendCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0 , 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49) collectionViewLayout:recommendLayout];
    self.recommendCollectionView.backgroundColor = [UIColor clearColor];
    self.recommendCollectionView.dataSource = self;
    self.recommendCollectionView.delegate = self;
    [self.view addSubview:self.recommendCollectionView];
    self.recommendCollectionView.showsVerticalScrollIndicator = NO;

    
    // 设置cell重用池
    [self.recommendCollectionView registerClass:[L_FirstRecommendCollectionViewCell class] forCellWithReuseIdentifier:@"posterCell"];
    [self.recommendCollectionView registerClass:[L_FirstRecommendCollectionViewCell class] forCellWithReuseIdentifier:@"firstCell"];
    [self.recommendCollectionView registerClass:[L_SecondRecommendCollectionViewCell class] forCellWithReuseIdentifier:@"secondCell"];
    [self.recommendCollectionView registerClass:[L_SecondRecommendCollectionViewCell class] forCellWithReuseIdentifier:@"thirdCell"];
    [self.recommendCollectionView registerClass:[L_SecondRecommendCollectionViewCell class] forCellWithReuseIdentifier:@"fourthCell"];
    [self.recommendCollectionView registerClass:[L_FirstRecommendCollectionViewCell class] forCellWithReuseIdentifier:@"fifthCell"];
    
    
    // 设置header重用池
    [self.recommendCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"recommendPosterHeader"];
    [self.recommendCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"recommendFirstHeader"];
    [self.recommendCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"recommendSecondHeader"];
    [self.recommendCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"recommendThirdHeader"];
    [self.recommendCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"recommendFourthHeader"];
    [self.recommendCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"recommendFifthHeader"];
    
    
}

#pragma mark - collectionView协议代理
// 设置sections个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 6;
}

// 设置item的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        return 0;
    } else if (section == 1) {
        return 1;
    } else if (section == 2) {
        return self.hotmusicArr.count;
    } else if (section == 3) {
        return self.selfdomRecommendArr.count;
    } else if (section == 4) {
        return self.smallRecommendArr.count;
    } else if (section == 5) {
        return self.AsianNewMusicArr.count;
    }
    return 0;
}

// 设置items内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        L_FirstRecommendCollectionViewCell *posterCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"posterCell" forIndexPath:indexPath];
        
        return posterCell;
    } else if (indexPath.section == 1) {
        L_FirstRecommendCollectionViewCell *firstCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"firstCell" forIndexPath:indexPath];
        firstCell.backgroundColor = RGBA(135, 135, 135, 0.8);
        firstCell.recommendModel = self.newestMusicArr[indexPath.row];
        return firstCell;
    } else if (indexPath.section == 2) {
        L_SecondRecommendCollectionViewCell *secondCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"secondCell" forIndexPath:indexPath];
        secondCell.backgroundColor = RGBA(135, 135, 135, 0.8);
        secondCell.recommendModel = self.hotmusicArr[indexPath.row];
        
        return secondCell;
    } else if (indexPath.section == 3) {
        L_SecondRecommendCollectionViewCell *thirdCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"thirdCell" forIndexPath:indexPath];
        thirdCell.backgroundColor = RGBA(135, 135, 135, 0.8);
        thirdCell.recommendModel = self.selfdomRecommendArr[indexPath.row];
        
        return thirdCell;
    } else if (indexPath.section == 4) {
        L_SecondRecommendCollectionViewCell *fourthCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"fourthCell" forIndexPath:indexPath];
        fourthCell.backgroundColor = RGBA(135, 135, 135, 0.8);
        fourthCell.recommendModel = self.smallRecommendArr[indexPath.row];
        
        return fourthCell;
    } else {
        L_FirstRecommendCollectionViewCell *fifthCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"fifthCell" forIndexPath:indexPath];
        fifthCell.backgroundColor = RGBA(135, 135, 135, 0.8);
        fifthCell.recommendModel = self.AsianNewMusicArr[indexPath.row];
        return fifthCell;
    }
    return nil;
}

// 设置items大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return CGSizeMake(10, 10);
    } else if (indexPath.section == 1) {
        return CGSizeMake(SCREEN_WIDTH - 20, SCREEN_HEIGHT / 7);
    } else if (indexPath.section == 2) {
        return CGSizeMake((SCREEN_WIDTH - 40) / 3, SCREEN_HEIGHT / 5 + 20);
    } else if (indexPath.section == 3) {
        return CGSizeMake((SCREEN_WIDTH - 40) / 2 - 20, SCREEN_HEIGHT / 5 + 20);
    } else if (indexPath.section == 4) {
        return CGSizeMake((SCREEN_WIDTH - 40) / 3, SCREEN_HEIGHT / 5);
    } else {
        return CGSizeMake(SCREEN_WIDTH - 20, SCREEN_HEIGHT / 7);
    }
}

// 设置header的内容
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        UICollectionReusableView *posterHeader = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"recommendPosterHeader" forIndexPath:indexPath];
        posterHeader.backgroundColor = [UIColor clearColor];
        
        UIView *bgView = [[UIView alloc] initWithFrame:posterHeader.bounds];
        [posterHeader addSubview:bgView];
        [bgView release];
        
        // 轮播图
        NSMutableArray *tempArr = [NSMutableArray array];
        for (L_PosterModel *poster in self.posterArr) {
            [tempArr addObject:poster.pic_url];
        }
        
        SDCycleScrollView *cycleView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 150) imageURLStringsGroup:tempArr];
        cycleView.delegate = self;
        [bgView addSubview:cycleView];

        
        return posterHeader;
    } else if (indexPath.section == 1) {
        UICollectionReusableView *firstHeader = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"recommendFirstHeader" forIndexPath:indexPath];

        // 创建头部视图
        L_TItleView *titleView = [[L_TItleView alloc] initWithFrame:firstHeader.bounds];

        titleView.label.text = [[self.titleArr objectAtIndex:2] objectForKey:@"name"];
        [firstHeader addSubview:titleView];
        [titleView release];

        return firstHeader;
    } else if (indexPath.section == 2) {
        UICollectionReusableView *secondHeader = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"recommendSecondHeader" forIndexPath:indexPath];
        
        // 创建头部视图
        L_TItleView *titleView = [[L_TItleView alloc] initWithFrame:secondHeader.bounds];
        titleView.label.text = [[self.titleArr objectAtIndex:1] objectForKey:@"name"];
        [secondHeader addSubview:titleView];
        [titleView release];

        return secondHeader;
    } else if (indexPath.section == 3) {
        UICollectionReusableView *thirdHeader = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"recommendThirdHeader" forIndexPath:indexPath];

        
        // 创建头部视图
        L_TItleView *titleView = [[L_TItleView alloc] initWithFrame:thirdHeader.bounds];
        titleView.label.text = [[self.titleArr objectAtIndex:5] objectForKey:@"name"];
        [thirdHeader addSubview:titleView];
        [titleView release];
        
        return thirdHeader;
    } else if (indexPath.section == 4) {
        UICollectionReusableView *fourthHeader = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"recommendFourthHeader" forIndexPath:indexPath];
//        fourthHeader.backgroundColor = [UIColor yellowColor];
        
        // 创建头部视图
        L_TItleView *titleView = [[L_TItleView alloc] initWithFrame:fourthHeader.bounds];
        titleView.label.text = [[self.titleArr objectAtIndex:3] objectForKey:@"name"];
        [fourthHeader addSubview:titleView];
        [titleView release];
        
        return fourthHeader;
    } else {
        UICollectionReusableView *fifthHeader = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"recommendFifthHeader" forIndexPath:indexPath];
//        fifthHeader.backgroundColor = [UIColor yellowColor];
        
        // 创建头部视图
        L_TItleView *titleView = [[L_TItleView alloc] initWithFrame:fifthHeader.bounds];
        titleView.label.text = [[self.titleArr objectAtIndex:4] objectForKey:@"name"];
        [fifthHeader addSubview:titleView];
        [titleView release];
        
        return fifthHeader;
    }
    return nil;
}

// 设置header的高度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return CGSizeMake(SCREEN_WIDTH, 150);
    } else if (section == 0) {
        return CGSizeMake(SCREEN_WIDTH, 30);
    } else if (section == 1) {
        return CGSizeMake(SCREEN_WIDTH, 30);
    } else if (section == 2) {
        return CGSizeMake(SCREEN_WIDTH, 30);
    } else if (section == 3) {
        return CGSizeMake(SCREEN_WIDTH, 30);
    } else if (section == 4) {
        return CGSizeMake(SCREEN_WIDTH, 30);
    } else {
        return CGSizeMake(SCREEN_WIDTH, 30);
    }
}


- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    if (section == 0) {
        UIEdgeInsetsMake(0, 0, 0, 0);
    } else if (section == 3) {
       return  UIEdgeInsetsMake(5, 20, 5, 20);
        
    }
    
    return UIEdgeInsetsMake(5, 10, 5, 10);
}


- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    if (section == 3) {
        return 20;
    }
    return 10;
}

#pragma mark - 轮播图点击方法
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{

    L_PosterModel *model = self.posterArr[index];
    
    NSMutableDictionary *dic = model.action;
    [self.Posterdelegate L_PosterPushViewController:index Action:dic];

    
    NSLog(@"~~~~%@", [self.posterArr[index] action]);

    
}


#pragma mark - collectionView点击方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
 
    
    L_PosterModel *m = nil;
    switch (indexPath.section) {
        case 1:
            
            m = self.newestMusicArr[indexPath.item];
            
            [self.delegate L_RecommendBarViewControllerPushViewController:indexPath SongId:[self.newestMusicArr[indexPath.item] songId]];
            break;
        case 2:
            [self.delegate L_RecommendBarViewControllerPushViewController:indexPath SongId:[self.hotmusicArr[indexPath.item] songId]];
            break;
        case 3:
            [self.delegate L_RecommendBarViewControllerPushViewController:indexPath SongId:[self.selfdomRecommendArr[indexPath.item] songId]];
            break;
        case 4:
            [self.delegate L_RecommendBarViewControllerPushViewController:indexPath SongId:[self.smallRecommendArr[indexPath.item] songId]];
            break;
        case 5:
            [self.delegate L_RecommendBarViewControllerPushViewController:indexPath Url:@"http://www.xiami.com/market/music/act/asia-song.php"];
            break;
            
        default:
            break;
    }
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
